<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <table class="layui-table" id="SysMenuTable" lay-filter="SysMenuTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="SysMenuTableToolBar">
    <div class="layui-btn-container">
        {php} if(in_array("admin/system.sysmenu/add",session('menunames'))){ {/php}
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add"><i class="layui-icon">&#xe654;</i>新增</button>
        {php} } {/php}
        {php} if(in_array("admin/system.sysmenu/edit",session('menunames'))){ {/php}
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</button>
        {php} } {/php}
        {php} if(in_array("admin/system.sysmenu/delete",session('menunames'))){ {/php}
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</button>
        {php} } {/php}
        <button class="layui-btn layui-btn-sm layui-btn-radius" lay-event="expandAll"><i class="layui-icon">&#xe654;</i>展开所有</button>
        <button class="layui-btn layui-btn-sm layui-btn-radius" lay-event="foldAll"><i class="layui-icon">&#xe656;</i>折叠所有</button>
    </div>
</script>
<script>
    var SysMenuRenderTable = function () {
        var loadingindex = layui.layer.msg('数据加载中...');
        layui.treetable.render({
            treeColIndex: 4,          // treetable新增参数
            treeSpid: 0,             // treetable新增参数
            treeIdName: 'id',       // treetable新增参数
            treePidName: 'pid',     // treetable新增参数
            treeDefaultClose: false,   // treetable新增参数
            treeLinkage: true,        // treetable新增参数
            elem: '#SysMenuTable',
            url: '{:url("/admin/system.sysmenu/index")}',
            method: "post",
            page: false,
            // skin: 'line',
            even: true, //开启隔行背景
            size: 'sm', //小尺寸的表格
            limit: '20',
            toolbar: "#SysMenuTableToolBar",
            defaultToolbar: ['filter', 'exports', 'print'],
            //defaultToolbar: [],
            loading: true,
            height: 'full-100',
            cellMinWidth: 100,
            cols: [[
                {type: 'radio'},
                {field: 'id', title: 'ID', width: 80},
                {field: 'pid', title: '父节点ID', width: 100},
                {
                    field: 'ismenu', width: 80, align: 'center', title: '类型', templet: function (d) {
                        if (d.ismenu == 1) {
                            return '<span class="layui-badge layui-bg-blue">菜单</span>';
                        } else {
                            return '<span class="layui-badge-rim">节点</span>';
                        }
                    }
                },
                {field: 'title', title: '名称', width: 250},
                {field: 'name', title: '权限规则'},
                {
                    field: 'status', width: 80, align: 'center', title: '状态', templet: function (d) {
                        if (d.status == 1) {
                            return '<span class="layui-badge layui-bg-blue">启用</span>';
                        } else {
                            return '<span class="layui-badge-rim">禁用</span>';
                        }
                    }
                },
                {field: 'icon', title: '图标', width: 150},
                {field: 'sort', title: '排序', width: 80},
                {field: 'condition', title: '附件条件'},
                {field: "", title: '操作', toolbar: "#operation", width: 100, fixed: 'right', align: 'center', hide: true},
            ]],
            done: function () {
                layui.layer.close(loadingindex);
            }
        });
    }
    SysMenuRenderTable();
    layui.table.on('toolbar(SysMenuTable)', function (obj) {
        //console.log(obj);
        let data = layui.table.checkStatus(obj.config.id).data[0];
        //console.log(data);
        if (obj.event == 'add') {
            $.get('{:url("/admin/system.sysmenu/edit")}', {}, function (str) {
                var index = layui.layer.open({
                    title: "修改",
                    offset: 't',
                    area: ['530px', '500px'],
                    shadeClose: true,
                    type: 1,
                    anim: parseInt(Math.random() * 6),
                    resize: false,
                    btn: ['保存', '取消'],
                    content: str,
                    yes: function (index, layero) {
                        //return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                        var data = $("#sysmenuForm").serializeObject();
                        var param = layui.dtree.getNowParam("parentIdTree");
                        console.log(param.nodeId);
                        data.pid = param.nodeId;
                        $.ajax({
                            async: true,//默认true为异步
                            url: '{:url("/admin/system.sysmenu/add")}',
                            type: "post",
                            data: data,
                            dataType: "json",
                            beforeSend: function (xhr) {
                            },
                            success: function (res, status, xhr) {
                                if (res.code == 1) {
                                    SysMenuRenderTable();
                                    layui.layer.close(index);
                                } else {
                                    layer.msg(res.msg);
                                }
                            },
                            error: function (xhr, status, error) {
                            }
                        });
                    },
                    success: function (layero, index) {
                    },
                    btn2: function (index, layero) {
                        //按钮【按钮二】的回调
                        //return false 开启该代码可禁止点击该按钮关闭
                    },
                    btn3: function (index, layero) {
                        //按钮【按钮三】的回调
                        //return false 开启该代码可禁止点击该按钮关闭
                    },
                    cancel: function () {
                        //右上角关闭回调
                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                })
            });
        }
        if (obj.event == 'edit') {
            if (data != null) {
                $.get('{:url("/admin/system.sysmenu/edit")}', {"id": data.id}, function (str) {
                    var index = layui.layer.open({
                        title: "修改",
                        offset: 't',
                        area: ['530px', '500px'],
                        shadeClose: true,
                        type: 1,
                        anim: parseInt(Math.random() * 6),
                        resize: false,
                        btn: ['保存', '取消'],
                        content: str,
                        yes: function (index, layero) {
                            //return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                            var data = $("#sysmenuForm").serializeObject();
                            var param = layui.dtree.getNowParam("parentIdTree");
                            data.pid = param.nodeId;
                            $.ajax({
                                async: true,//默认true为异步
                                url: '{:url("/admin/system.sysmenu/edit")}',
                                type: "post",
                                data: data,
                                dataType: "json",
                                beforeSend: function (xhr) {
                                },
                                success: function (res, status, xhr) {
                                    if (res.code == 1) {
                                        SysMenuRenderTable();
                                        layui.layer.close(index);
                                    } else {
                                        layer.msg('失败！请重试！');
                                    }
                                },
                                error: function (xhr, status, error) {
                                }
                            });
                        },
                        success: function (layero, index) {
                            var enhance = new layui.enhanceform({
                                elem: '#sysmenuForm' //表单选择器
                            });
                            enhance.filling(data);//其中jsonData为表单数据的json对象
                        },
                        btn2: function (index, layero) {
                            //按钮【按钮二】的回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        },
                        btn3: function (index, layero) {
                            //按钮【按钮三】的回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        },
                        cancel: function () {
                            //右上角关闭回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    })
                });
            } else {
                layer.msg('请勾选记录！');
            }
        }
        if (obj.event == 'delete') {
            if (data != null) {
                layui.layer.confirm('确定删除此信息？', {icon: 2, title: '提示信息'}, function (index) {
                    $.ajax({
                        type: "post",
                        url: '{:url("/admin/system.sysmenu/delete")}',
                        data: {'id': data.id},
                        dataType: 'json',
                        success: function (d) {
                            layui.layer.msg("删除成功");
                            SysMenuRenderTable();
                        }
                    })
                    layer.close(index);
                });
            } else {
                layer.msg('请勾选记录！');
            }
        }
        if (obj.event == 'expandAll') {
            layui.treetable.expandAll('#SysMenuTable');
        }
        if (obj.event == 'foldAll') {
            layui.treetable.foldAll('#SysMenuTable');
        }
    });
    layui.table.on('tool(SysMenuTable)', function (obj) {
        console.log(obj);
        //var data = layui.table.checkStatus(obj.config.id);
        //console.log(data);
        //var data = obj.data;
        if (obj.event == 'off') {
            self.location.href = 'troubleclose.html?id=' + data.id;
        }
    });
</script>
